package com.banyou.banyou.main;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.banyou.banyou.R;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.main.list.ListFragment;
import com.banyou.banyou.main.my.MyFragment;
import com.banyou.banyou.main.search.SearchFragment;
import com.banyou.banyou.util.SnackBarUtil;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {
    @BindView(R.id.navigation_bar)
    BottomNavigationBar mNavigationBar;
    @BindView(R.id.fragment_content)
    RelativeLayout mFragmentContent;
    private MyFragment mMyFragment;
    private SearchFragment mSearchFragment;
    private ListFragment mListFragment;
    private List<Fragment> mList;
    private int mIndex=0;
    private FragmentManager mManager;
    private final String MY = "my", SEARCH = "search", LIST = "list";

    @Override
    protected MainPresenter initPresenter() {
        return new MainPresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {

    }

    @Override
    public void init(Bundle savedInstanceState) {
        mPresenter.initPresenter(this, new MainModel());
        ButterKnife.bind(this);
        initNavigation();
        loadFragemnt(savedInstanceState);

    }

    /**
     * 初始化底部导航
     */
    private void initNavigation() {
        BottomNavigationItem serch = new BottomNavigationItem(R.drawable.serch_blue, "搜索");
        serch.setActiveColor(R.color.color_479aff);
        serch.setInactiveIconResource(R.drawable.serch_normal);
        BottomNavigationItem list = new BottomNavigationItem(R.drawable.list_blue, "行程单");
        list.setActiveColor(R.color.color_479aff);
        list.setInactiveIconResource(R.drawable.list_normal);
        BottomNavigationItem me = new BottomNavigationItem(R.drawable.me_blue, "我");
        me.setActiveColor(R.color.color_479aff);
        me.setInactiveIconResource(R.drawable.me_normal);


        mNavigationBar.setMode(BottomNavigationBar.MODE_DEFAULT);
        mNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mNavigationBar.addItem(serch)
                .addItem(list)
                .addItem(me)
                .initialise();
        mNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                if (mIndex == position) {
                    return;
                }
                KLog.i("position="+position);
                FragmentTransaction transaction = mManager.beginTransaction();
                transaction.show(mList.get(position));
                transaction.commit();
                mIndex=position;
            }

            @Override
            public void onTabUnselected(int position) {
                FragmentTransaction transaction = mManager.beginTransaction();
                transaction.hide(mList.get(position));
                transaction.commit();
            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putInt("index", mIndex);
        super.onSaveInstanceState(outState, outPersistentState);
    }

    /**
     * 加载Fragment
     */
    private void loadFragemnt(Bundle savedInstanceState) {
        mList = new ArrayList<>();
        mManager = getSupportFragmentManager();
        FragmentTransaction transaction = mManager.beginTransaction();
        if (savedInstanceState == null) {
            mMyFragment = new MyFragment();
            mSearchFragment = new SearchFragment();
            mListFragment = new ListFragment();
            mList.add(mSearchFragment);
            mList.add(mListFragment);
            mList.add(mMyFragment);
            transaction.add(R.id.fragment_content, mMyFragment, MY);
            transaction.add(R.id.fragment_content, mSearchFragment, SEARCH);
            transaction.add(R.id.fragment_content, mListFragment, LIST);
        } else {
            mIndex = savedInstanceState.getInt("index");
            mSearchFragment = (SearchFragment) mManager.findFragmentByTag(SEARCH);
            mListFragment = (ListFragment) mManager.findFragmentByTag(LIST);
            mMyFragment = (MyFragment) mManager.findFragmentByTag(MY);
            mList.add(mSearchFragment);
            mList.add(mListFragment);
            mList.add(mMyFragment);
        }
        if (mIndex == 0) {
            transaction.hide(mListFragment);
            transaction.hide(mMyFragment);
        } else if (mIndex == 1) {
            transaction.hide(mSearchFragment);
            transaction.hide(mMyFragment);
        } else if (mIndex == 2) {
            transaction.hide(mSearchFragment);
            transaction.hide(mListFragment);
        }
        transaction.commit();
    }


    @Override
    protected String pageName() {
        return "主页面";
    }

    /**
     * 设置返回键点击事件
     * 2秒内两次连续点击后退键则退出程序
     *
     * @param keyCode
     * @param event
     * @return
     */
    private int count = 0;
    private BackThread backThread = null;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            doubleTap();//连续点击检测
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 连续点击检测
     */
    private void doubleTap() {
        if (count == 0) {
            SnackBarUtil.makeSnackBar("再次点击退出程序");
            backThread = null;
            backThread = new BackThread();
            backThread.start();
        }
        count++;
        if (count == 2) {
            MainActivity.this.finish();
            backThread = null;
        }
    }


    private class BackThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count = 0;
        }
    }

}
